<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>LinkedHashMap的顺序保证机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            border-radius: 8px;
            overflow-x: auto;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1.5rem;
            border-radius: 12px;
            margin: 2rem 0;
        }
        
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="text-center">
                <h1 class="text-5xl font-bold mb-6 floating-icon">
                    <i class="fas fa-link mr-4"></i>LinkedHashMap
                </h1>
                <p class="text-2xl mb-4">如何保证元素的顺序不变</p>
                <p class="text-lg opacity-90 max-w-3xl mx-auto">
                    深入探索Java集合框架中LinkedHashMap的内部机制，理解它如何通过双向链表维护元素顺序，
                    以及在LRU缓存等场景中的实际应用。
                </p>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <div class="flex items-start">
                <div class="text-4xl mr-6 feature-icon">
                    <i class="fas fa-info-circle"></i>
                </div>
                <div class="flex-1">
                    <h2 class="text-2xl font-bold mb-4">概述</h2>
                    <p class="text-gray-700 leading-relaxed text-lg">
                        在Java集合框架中，LinkedHashMap是一种特殊的Map实现，它继承自HashMap并在内部通过双向链表维护元素的顺序。
                        通常，我们在讨论HashMap时关注的是高效的键值存取和良好的时间复杂度，但LinkedHashMap在此基础上增加了"顺序性"的特性，
                        使得它在某些场景下更具优势。
                    </p>
                </div>
            </div>
        </div>

        <!-- Core Features Grid -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="content-card rounded-xl shadow-lg p-6">
                <div class="flex items-center mb-4">
                    <div class="text-3xl mr-4 feature-icon">
                        <i class="fas fa-sort-amount-down"></i>
                    </div>
                    <h3 class="text-xl font-bold">插入顺序模式</h3>
                </div>
                <p class="text-gray-700 mb-4">
                    默认情况下，LinkedHashMap按键的插入顺序存储元素。无论元素被访问多少次，
                    其遍历顺序都与插入的先后顺序一致。
                </p>
                <div class="bg-purple-50 rounded-lg p-4">
                    <p class="text-sm text-purple-800">
                        <i class="fas fa-lightbulb mr-2"></i>
                        适用于历史记录、任务队列等需要记录先后顺序的场景
                    </p>
                </div>
            </div>

            <div class="content-card rounded-xl shadow-lg p-6">
                <div class="flex items-center mb-4">
                    <div class="text-3xl mr-4 feature-icon">
                        <i class="fas fa-history"></i>
                    </div>
                    <h3 class="text-xl font-bold">访问顺序模式</h3>
                </div>
                <p class="text-gray-700 mb-4">
                    通过设置accessOrder为true，可以实现访问顺序模式。每次访问某个元素时，
                    将其移至队列的末尾，实现按访问频次排序。
                </p>
                <div class="bg-purple-50 rounded-lg p-4">
                    <p class="text-sm text-purple-800">
                        <i class="fas fa-lightbulb mr-2"></i>
                        完美支持LRU（最近最少使用）缓存机制的实现
                    </p>
                </div>
            </div>
        </div>

        <!-- Data Structure Visualization -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-2xl font-bold mb-6">内部数据结构</h2>
            <div class="mermaid">
                <script type="module">
                    import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.esm.min.mjs';
                    mermaid.initialize({ startOnLoad: true });
                </script>
                <div class="mermaid">
                    graph LR
                        A[HashMap数组] --> B[Entry节点1]
                        A --> C[Entry节点2]
                        A --> D[Entry节点3]
                        B -.->|before| E[双向链表]
                        C -.->|before/after| E
                        D -.->|after| E
                        E --> F[保持顺序]
                        style A fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                        style E fill:#764ba2,stroke:#333,stroke-width:2px,color:#fff
                        style F fill:#f093fb,stroke:#333,stroke-width:2px,color:#fff
                </div>
            </div>
            <div class="mt-6 grid md:grid-cols-3 gap-4">
                <div class="bg-gray-50 rounded-lg p-4">
                    <h4 class="font-bold text-purple-700 mb-2">
                        <i class="fas fa-database mr-2"></i>继承HashMap
                    </h4>
                    <p class="text-sm text-gray-600">
                        重用HashMap的核心结构，提供高效的键值对存储和查找
                    </p>
                </div>
                <div class="bg-gray-50 rounded-lg p-4">
                    <h4 class="font-bold text-purple-700 mb-2">
                        <i class="fas fa-arrows-alt-h mr-2"></i>双向链表
                    </h4>
                    <p class="text-sm text-gray-600">
                        每个节点包含before和after指针，形成双向链表结构
                    </p>
                </div>
                <div class="bg-gray-50 rounded-lg p-4">
                    <h4 class="font-bold text-purple-700 mb-2">
                        <i class="fas fa-cog mr-2"></i>Entry扩展
                    </h4>
                    <p class="text-sm text-gray-600">
                        LinkedHashMap.Entry继承自HashMap.Node，增加链表指针
                    </p>
                </div>
            </div>
        </div>

        <!-- Implementation Details -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-2xl font-bold mb-6">核心实现机制</h2>
            
            <div class="space-y-6">
                <div class="border-l-4 border-purple-500 pl-6">
                    <h3 class="text-lg font-bold mb-2">主要内部方法</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-purple-500 mt-1 mr-3"></i>
                            <div>
                                <strong>afterNodeAccess()</strong> - 当accessOrder为true时，每次访问元素后调用，
                                将被访问的节点移到链表末尾
                            </div>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-purple-500 mt-1 mr-3"></i>
                            <div>
                                <strong>afterNodeInsertion()</strong> - 插入新节点时调用，
                                可触发removeEldestEntry()判断是否移除最老元素
                            </div>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-purple-500 mt-1 mr-3"></i>
                            <div>
                                <strong>removeEldestEntry()</strong> - 用户可重写此方法控制是否删除最老元素，
                                实现LRU缓存的关键
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- LRU Cache Implementation -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-2xl font-bold mb-6">LRU缓存实现</h2>
            
            <div class="highlight-box mb-6">
                <h3 class="text-xl font-bold mb-2">
                    <i class="fas fa-code mr-2"></i>完整的LRU缓存示例
                </h3>
                <p>基于LinkedHashMap实现的简单高效的LRU缓存，支持自动淘汰最久未使用的数据</p>
            </div>

            <pre class="code-block p-6 text-sm overflow-x-auto"><code>import java.util.LinkedHashMap;
import java.util.Map;

public class LRUCache&lt;K, V&gt; extends LinkedHashMap&lt;K, V&gt; {
    private final int capacity;

    // 构造函数，初始化缓存容量并设置accessOrder为true
    public LRUCache(int capacity) {
        super(capacity, 0.75f, true); // true表示按访问顺序
        this.capacity = capacity;
    }

    // 重写removeEldestEntry方法，控制缓存大小
    @Override
    protected boolean removeEldestEntry(Map.Entry&lt;K, V&gt; eldest) {
        return size() > capacity; // 超过容量时移除最久未使用的元素
    }

    // 使用示例
    public static void main(String[] args) {
        LRUCache&lt;Integer, String&gt; cache = new LRUCache&lt;&gt;(3);
        cache.put(1, "A");
        cache.put(2, "B");
        cache.put(3, "C");
        
        cache.get(1); // 访问key=1
        cache.put(4, "D"); // 插入新元素，key=2将被移除
        // 缓存内容：{3=C, 1=A, 4=D}
    }
}</code></pre>
        </div>

        <!-- Use Cases -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-2xl font-bold mb-6">典型应用场景</h2>
            
            <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-lg p-6 hover:shadow-lg transition-shadow">
                    <div class="text-3xl mb-3 text-purple-600">
                        <i class="fas fa-server"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">Web服务器缓存</h3>
                    <p class="text-gray-700 text-sm">
                        缓存静态资源、数据库查询结果，减少I/O操作，提升响